package com.mr;

import com.mr.utils.EsUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;

import java.io.IOException;

public class testindex {
    private RestHighLevelClient restHighLevelClient = EsUtils.getClient();
    private  String index = "person";
    @Test
    private void Testindex() throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
        Settings.Builder settings = Settings.builder()
                .put("number_of_shards", 5)
                .put("number_of_replicas", 1);
        createIndexRequest.settings(settings);
        XContentBuilder mappings = JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("name")
                .field("type","text")
                .field("type","ik_max_word")
                .endObject()
                .startObject("age")
                .field("type","integer")
                .endObject()
                .startObject("birthday")
                .field("type","date")
                .field("formar","yyyy-MM-dd")
                .endObject()
                .endObject()
                .endObject();
        createIndexRequest.mapping(mappings);
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.out.println(
                createIndexRequest.index()

        );


    }
    @Test
    private void testseIndex()throws Exception{
        GetIndexRequest request  = new GetIndexRequest(index);

        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);


    }
    //删除
    private void testDeleteIndex()throws Exception{
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);

        AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);



    }
}
